﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VietKinsNutrition.Bussiness;
using VietKinsNutrition.Common;

namespace VietKinsNutrition.ThuVien
{
    public partial class FrmTblDm_Capnhatthucpham : DevExpress.XtraEditors.XtraForm
    {
        ClsTblDm_CapnhatthucphamBLL ClsTblDm_Capnhatthucphambll = new ClsTblDm_CapnhatthucphamBLL();

        private int Option;
        private int iD;
        private string tenThucPham;
        public delegate void GetFunction();
        public delegate void Update();
        public GetFunction CallFunction;
        public Update GetName;
        private TextEdit[] txt = new TextEdit[]{};
        private CheckEdit[] check = new CheckEdit[]{};

        public FrmTblDm_Capnhatthucpham(string Name, string ID, string idNhom, int idLuaNhomTP)
        {
            InitializeComponent();
            if (ID != "")
            {
                Option = 2;
                tenThucPham = Name;
                txt = new TextEdit[] { txtTenTP, this.txtTLquydoi, this.txtTLthai, txtNuoc, txtDam, txtBeo, txtDuong, txtXo, txtCholesterol, txtCanxi, txtPhotpho, txtSat, txtCaroten, txtVA, txtB1, txtB2, txtPP, txtVC, txtNatri, txtFomic, txtKali };
                check = new CheckEdit[] { this.chkDongvat, this.chkNguyenlieu };
                iD = int.Parse(ID);
            }
            else
            {
                Option = 1;
                txt = new TextEdit[] { txtTenTP, this.txtTLquydoi, this.txtTLthai, txtNuoc, txtDam, txtBeo, txtDuong, txtXo, txtCholesterol, txtCanxi, txtPhotpho, txtSat, txtCaroten, txtVA, txtB1, txtB2, txtPP, txtVC, txtNatri, txtFomic, txtKali };
                check = new CheckEdit[] { this.chkDongvat, this.chkNguyenlieu };
                ClsTblDm_Capnhatthucphambll.Creats(txt, check);
                this.txtTenTP.Text = "";
                if (idLuaNhomTP != -1)
                {
                    lueNhomTP.EditValue = idLuaNhomTP;
                }
            }
        }

        private void FrmTblDm_Capnhatthucpham_Load(object sender, EventArgs e)
        {
            this.dateEdit1.EditValue = DateTime.Now.ToString("dd/MM/yy");
            ClsTblDm_Capnhatthucphambll.FillComboBox(lueNhomTP, cboDonvi);
            if (Option == 2)
            {
                ClsTblDm_Capnhatthucphambll.Display(iD, txt, txtNangluong, this.lueNhomTP, this.cboDonvi, check, this.dateEdit1);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboDonvi_SelectedValueChanged(object sender, EventArgs e)
        {
            lbdvt.Text = "1 " + cboDonvi.SelectedText.ToString() + " ~ 1 kilogam";
        }

        private void btnDongy_Click(object sender, EventArgs e)
        {
            //ComboBox[] combo = new ComboBox[] { this.comboNhomThucAn, this.comboDonViTinh };
            if(!ClsTblDm_Capnhatthucphambll.CheckCombo(this.lueNhomTP, this.cboDonvi))
            {
                return;
            }
            int[] idCombo = new int[] { (int)this.lueNhomTP.EditValue, Convert.ToInt32(UserComboBoxItem.GetItemValue(this.cboDonvi)) };
            TextEdit[] txt = new TextEdit[] { this.txtTLquydoi, this.txtTLthai, txtNuoc, txtDam, txtBeo, txtDuong, txtXo, txtCholesterol, txtCanxi, txtPhotpho, txtSat, txtCaroten, txtVA, txtB1, txtB2, txtPP, txtVC, txtNatri, txtFomic, txtKali };
            CheckEdit[] check = new CheckEdit[] { this.chkNguyenlieu, this.chkDongvat };
            if (ClsTblDm_Capnhatthucphambll.DataBase(iD, Option, tenThucPham, this.txtTenTP, idCombo, txt, check, this.txtNangluong, this.dateEdit1))
            {
                if (CallFunction != null)
                {
                    CallFunction();
                }
                if (GetName != null)
                {
                    GetName();
                }
                this.Close();
            }
        }

        private void chkNguyenlieu_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNguyenlieu.Checked)
                chkDongvat.Checked = false;
            else
                chkDongvat.Checked = true;
        }

        private void chkDongvat_CheckedChanged(object sender, EventArgs e)
        {
            if(chkDongvat.Checked)
                chkNguyenlieu.Checked = false;
            else
                chkNguyenlieu.Checked = true;
        }
    }
}